package algorithm.poj.p1000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;

/**
 * 分析：
 * 实现：
 * 经验：
 * 教训：
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P1146 {

	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P1146.class.getResource("P1146.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		StringBuffer sb = new StringBuffer("");
		String line = stdin.readLine();
		while (line != null && !"#".equals(line)) {
			char[] s = next_permutation(line.trim().toCharArray());
			if (s == null) {
				sb.append("No Successor\n");
			} else {
				sb.append(new String(s)).append("\n");
			}
			line = stdin.readLine();
		}
		System.out.println(sb);
	}
	
	public static char[] next_permutation(char[] ds) {
		
		int j = ds.length-2;
		for (; j > -1; j --) {
			if (ds[j+1] > ds[j]) break;
		}
		if (j == -1) {
			return null;
		} else {
			int k = ds.length-1;
			for (; k > -1; k --) {
				if (ds[k] > ds[j]) break;
			}
			char t = ds[k];
			ds[k] = ds[j];
			ds[j] = t;
			for (int i = j+1; i < (ds.length+j)/2+1; i ++) {
				char s = ds[ds.length+j-i];
				ds[ds.length+j-i] = ds[i];
				ds[i] = s;
			}
		}
		return ds;
	}
}